import { BasicColumn, FormSchema } from '@/components/Table';
import {h} from "vue";
import {Switch} from "ant-design-vue";
import {useMessage} from "@/hooks/web/useMessage";
import { updateAdminUser } from "@/api/admin/admin_user";
import {getAdminGroupOptions} from "@/api/common";

/**
 * transform mock data
 * {
 *  0: '华东分部',
 * '0-0': '华东分部-研发部'
 * '0-1': '华东分部-市场部',
 *  ...
 * }
 */
export const deptMap = (() => {
  const pDept = ['华东分部', '华南分部', '西北分部'];
  const cDept = ['研发部', '市场部', '商务部', '财务部'];

  return pDept.reduce((map, p, pIdx) => {
    map[pIdx] = p;

    cDept.forEach((c, cIndex) => (map[`${pIdx}-${cIndex}`] = `${p}-${c}`));

    return map;
  }, {});
})();

export const columns = [
  {
    title: '权限组',
    dataIndex: 'group_title',
    width: 100,
  },
  {
    title: '用户名',
    dataIndex: 'username',
    width: 120,
  },
  {
    title: '状态',
    dataIndex: 'status',
    align: 'center',
    width: 80,
    customRender: ({ record }) => {
      if (!Reflect.has(record, 'pendingStatus')) {
        record.pendingStatus = false
      }
      return h(Switch, {
        checked: record.status === 'enable',
        disabled: false,
        // disabled: !hasPermission(['system_role_changeState']),
        checkedChildren: '正常',
        unCheckedChildren: '禁用',
        loading: record.pendingStatus,
        onChange(checked: boolean) {
          record.pendingStatus = true
          const newStatus = checked ? 'enable' : 'disable'
          const { createMessage } = useMessage()
          updateAdminUser({ ...record, status: newStatus })
            .then(() => {
              record.status = newStatus
              createMessage.success(`状态修改成功`)
            })
            .catch(() => {
              createMessage.error('状态修改失败')
            })
            .finally(() => {
              record.pendingStatus = false
            })
        },
      })
    },
  },
  {
    title: '最后更新',
    dataIndex: 'update_time',
  },
];

export const searchFormSchema: FormSchema[] = [
  {
    field: 'account',
    label: '用户名',
    component: 'Input',
    colProps: { span: 8 },
  },
  {
    field: 'nickname',
    label: '昵称',
    component: 'Input',
    colProps: { span: 8 },
  },
];

export const accountFormSchema: FormSchema[] = [
  {
    field: 'group_id',
    label: '权限组',
    component: 'ApiSelect',
    componentProps: {
      api: getAdminGroupOptions,
      resultField: 'result.list',
      labelField: 'title',
      valueField: 'id',
    },
    required: true,
  },
  {
    field: 'username',
    label: '用户名',
    component: 'Input',
    helpMessage: ['本字段演示异步验证', '不能输入带有admin的用户名'],
    rules: [
      {
        required: true,
        message: '请输入用户名',
      },
    ],
  },
  {
    field: 'password',
    label: '密码',
    component: 'InputPassword',
    required: true,
    ifShow: false,
  },
  {
    field: 'password1',
    label: '确认密码',
    component: 'InputPassword',
    required: true,
    ifShow: false,
  },
  {
    label: '手机号',
    field: 'phone',
    component: 'Input',
    componentProps: {
      maxlength: 11,
      showCount: true,
    },
    required: false,
  },
  {
    label: '邮箱',
    field: 'email',
    component: 'Input',
    required: false,
  },
];
export const changePasswordFormSchema: FormSchema[] = [
  {
    field: 'password',
    label: '密码',
    component: 'StrengthMeter',
    helpMessage: ['密码不符合规范，须包含:字母、数字、特殊字符，8~16位长度'],
    componentProps: {
      placeholder: '请输入密码',
    },
    required: true,
  },
  {
    field: 'password1',
    label: '确认密码',
    component: 'InputPassword',
    helpMessage: ['密码不符合规范，须包含:字母、数字、特殊字符，8~16位长度'],
    componentProps: {
      placeholder: '请输入密码',
    },
    required: true,
  },
]
